const multer = require('@koa/multer')
const {
	UPLOAD_PATH,
	UPLOAD_POST_PICTURE_PATH,
	UPLOAD_EVENT_PICTURE_PATH
} = require('../config/path')

const uploadAvatar = multer({
	storage: multer.diskStorage({
		destination(req, file, cb) {
			console.log(req)
			cb(null, UPLOAD_PATH)
		},
		filename(req, file, cb) {
			cb(null, Date.now() + '_' + file.originalname)
		}
	})
})

const uploadPostPicture = multer({
	storage: multer.diskStorage({
		destination(req, file, cb) {
			console.log(req)
			cb(null, UPLOAD_POST_PICTURE_PATH)
		},
		filename(req, file, cb) {
			cb(null, Date.now() + '_' + file.originalname)
		}
	})
})

const uploadEventPicture = multer({
	storage: multer.diskStorage({
		destination(req, file, cb) {
			console.log(req, 123)
			cb(null, UPLOAD_EVENT_PICTURE_PATH)
		},
		filename(req, file, cb) {
			cb(null, Date.now() + '_' + file.originalname)
		}
	})
})

const handleAvatar = uploadAvatar.single('avatar')
const handlePostPicture = uploadPostPicture.single('post-picture')
const handleEventPicture = uploadEventPicture.single('event-picture')
module.exports = {
	handleAvatar,
	handlePostPicture,
	handleEventPicture
}
